const {app, BrowserWindow, Menu, shell, dialog} = require('electron')
const mainProcess = require('./main') // 这里没有用remote 哦


const creatApplicationMenu = () => {
  // 检查是否有任何打开的窗口
  const hasOneOrMoreWindows = !!BrowserWindow.getAllWindows().length
  // 获取当前焦点窗口
  const focusedWindow = BrowserWindow.getFocusedWindow()
  // 如果有焦点窗口，调用它的getRepresentedFilename方法，返回所打开文件的路径。
  const hasFilePath = focusedWindow && focusedWindow.getRepresentedFilename()
  console.log(hasFilePath)
  const template = [
    {
      label: 'File',
      submenu: [
        {
          label: 'New File',
          accelerator: 'CommandOrControl + N',
          click() {
            mainProcess.creatWindow()
          }
        },
        {
          label: 'Open File',
          accelerator: 'CommandOrControl + O',
          click(item, focusedWindow) {
            if (!focusedWindow) {
              const newWindow = mainProcess.creatWindow()
              newWindow.on('show', () => {
                mainProcess.getFileFromUser(newWindow)
              })
            }
            mainProcess.getFileFromUser(focusedWindow)
          }
        },
        {
          label: 'Save File',
          accelerator: 'CommandOrControl + S',
          click(item, focusedWindow) {
            // 没有焦点窗口时响应
            if (!focusedWindow) {
              return dialog.showErrorBox(
                `Cannot Save or Export,
                 There is currently no active document to save or export
                `
              )
            }
            focusedWindow.webContents.send('save-markdown')
          },
          enabled: hasOneOrMoreWindows
        },
        {
          label: 'Export HTML',
          accelerator: 'Shift + CommandOrControl + S',
          // 没有焦点窗口时响应
          click(item, focusedWindow) {
            if(!focusedWindow) {
              return dialog.showErrorBox(
                `Cannot Save or Export,
                 There is currently no active document to save or export
                `
              )
            }
            focusedWindow.webContents.send('save-html')
          },
          enabled: hasOneOrMoreWindows
        },
        {type: 'separator'},
        {
          label: 'Show File',
          accelerator: 'Shift + CommandOrControl + S',
          click(item, focusedWindow) {
            if (!focusedWindow) {
              // 如果没有焦点窗口，向用户显示错误信息，从函数返回
              return dialog.showErrorBox(
                `Cannot show File's Location
                There is currently no active document show
                `
              )
            }
            focusedWindow.webContents.send('show-file')
          },
          enabled: hasFilePath
        },
        {
          label: 'Open in Default Editor',
          accelerator: 'Shift + CommandOrControl + S',
          click(item, focusedWindow) {
            if(!focusedWindow) {
              return dialog.showErrorBox(
                `Cannot Open File in Default Editor,
                There is currently no active document to open
                `
              )
            }
            focusedWindow.webContents.send('open-in-default')
          },
          enabled: hasFilePath
        }
      ]
    },
    {
      label: 'Edit',
      submenu: [
        {
          label: 'Undo',
          accelerator: 'CommandOrControl + Z',
          role: 'undo'
        },
        {
          label: 'Redo',
          accelerator: 'Shift + CommandOrControl + Z',
          role: 'redo'
        },
        {type: 'separator'},
        {
          label: 'Cut',
          accelerator: 'CommandOrControl + X',
          role: 'cut'
        },
        {
          label: 'Copy',
          accelerator: 'CommandOrControl + C',
          role: 'copy'
        },
        {
          label: 'Paster',
          accelerator: 'CommandOrControl + V',
          role: 'paste'
        },
        {
          label: 'Select All',
          accelerator: 'CommandOrControl + A',
          role: 'selectall'
        }
      ]
    },
  
    {
      label: 'Window',
      submenu: [
        {
          label: 'Minimize',
          accelerator: 'CommandOrControl + M',
          role: 'minimize'
        },
        {
          label: 'Close',
          accelerator: 'CommandOrControl + W',
          role: 'close'
        }
      ]
    },
    {
      label: 'Help',
      role: 'help',
      submenu: [
        {
          label: 'Visit Website',
          click() {}
        },
        {
          label: 'Toggle Developer Tools',
          click(item, focusedWindow) {
            if (focusedWindow) {
              focusedWindow.webContents.toggleDevTools()
            }
          }
        }
      ]
    }
  ]
  if (process.platform === 'darwin') {
    const name = 'Fire Sale'
    template.unshift({
      label: name,
      submenu: [
        {
          label: `About ${name}`,
          role: 'about'
        },
        {type: 'separator'},
        {
          label: `Services ${name}`,
          role: 'services',
          submenu: []
        },
        {type: 'separator'},
        {
          label: `Hide ${name}`,
          accelerator: 'Command + H',
          role: 'hide'
        },
        {
          label: 'Hide Others',
          accelerator: 'Command + Alt + H',
          role: 'hideothers'
        },
        {
          label: `Show All ${name}`,
          role: 'unhide'
        },
        {type: 'separator'},
        {
          label: `Quit ${name}`,
          accelerator: 'Command + Q',
          click() {app.quit()}
        }
  
      ]
    })
  }
  
  
  const windowMenu = template.find(item => item.label === 'Window')
  windowMenu.role = 'window'
  windowMenu.submenu.push(
    {type: 'separator'},
    {
      label: 'Bring All to Front',
      role: 'front'
    }
  )
  return Menu.setApplicationMenu(Menu.buildFromTemplate(template))
}
module.exports = creatApplicationMenu
